package com.hupu.jrs

/**
 * Created by sll on 2016/6/30.
 */

/**
 * Wrapper API for sending log output.
 */
object Logger {
    val TAG = "TLint"
    var DEBUG = true

    /**
     * Send a VERBOSE log message.
     *
     * @param msg The message you would like logged.
     */
    fun v(msg: String) {
        if (DEBUG) android.util.Log.v(TAG, buildMessage(msg))
    }

    /**
     * Send a VERBOSE log message and log the exception.
     *
     * @param msg The message you would like logged.
     * @param thr An exception to log
     */
    fun v(msg: String, thr: Throwable) {
        if (DEBUG) android.util.Log.v(TAG, buildMessage(msg), thr)
    }

    /**
     * Send a DEBUG log message.
     */
    fun d(msg: String) {
        if (DEBUG) android.util.Log.d(TAG, buildMessage(msg))
    }

    /**
     * Send a DEBUG log message and log the exception.
     *
     * @param msg The message you would like logged.
     * @param thr An exception to log
     */
    fun d(msg: String, thr: Throwable) {
        if (DEBUG) android.util.Log.d(TAG, buildMessage(msg), thr)
    }

    /**
     * Send an INFO log message.
     *
     * @param msg The message you would like logged.
     */
    fun i(msg: String) {
        if (DEBUG) android.util.Log.i(TAG, buildMessage(msg))
    }

    /**
     * Send a INFO log message and log the exception.
     *
     * @param msg The message you would like logged.
     * @param thr An exception to log
     */
    fun i(msg: String, thr: Throwable) {
        if (DEBUG) android.util.Log.i(TAG, buildMessage(msg), thr)
    }

    /**
     * Send an ERROR log message.
     *
     * @param msg The message you would like logged.
     */
    fun e(msg: String) {
        if (DEBUG) android.util.Log.e(TAG, buildMessage(msg))
    }

    /**
     * Send a WARN log message
     *
     * @param msg The message you would like logged.
     */
    fun w(msg: String) {
        if (DEBUG) android.util.Log.w(TAG, buildMessage(msg))
    }

    /**
     * Send a WARN log message and log the exception.
     *
     * @param msg The message you would like logged.
     * @param thr An exception to log
     */
    fun w(msg: String, thr: Throwable) {
        if (DEBUG) android.util.Log.w(TAG, buildMessage(msg), thr)
    }

    /**
     * Send an empty WARN log message and log the exception.
     *
     * @param thr An exception to log
     */
    fun w(thr: Throwable) {
        if (DEBUG) android.util.Log.w(TAG, buildMessage(""), thr)
    }

    /**
     * Send an ERROR log message and log the exception.
     *
     * @param msg The message you would like logged.
     * @param thr An exception to log
     */
    fun e(msg: String, thr: Throwable) {
        if (DEBUG) android.util.Log.e(TAG, buildMessage(msg), thr)
    }

    /**
     * Building Message
     *
     * @param msg The message you would like logged.
     * @return Message String
     */
    fun buildMessage(msg: String): String {
        val caller = Throwable().fillInStackTrace().stackTrace[2]

        return StringBuilder().append(caller.fileName)
                .append(".")
                .append(caller.methodName)
                .append("(): ")
                .append(msg)
                .toString()
    }
}
